package hojadetrabajo4;

public class CircularList<E> extends abstractList<E>
{
protected Node<E> tail; 
protected int count;

    /**
     * Pre-condicion: constructs a new circular list
     */
    public CircularList()
    
    {
       tail = null;
       count = 0;
    }
    /**
     * Pre-condicion: value non-null
     * Post-condicion: adds element to head of list
     */ 
    public void push(E value)
    
    {
       Node<E> temp = new Node<E>(value);
       if (tail == null) { // first value added
           tail = temp;
           tail.setNext(tail);
       } else { // element exists in list
           temp.setNext(tail.next());
           tail.setNext(temp);
       }
       count++;
    }

    /**
     * Pre-condicion: !isEmpty()
     * Post-condicion: returns and removes value from tail of list
     * @return 
     */
    public E pop()
    
    {
       Node<E> finger = tail;
       while (finger.next() != tail) {
           finger = finger.next();
       }
       // finger now points to second-to-last value
       Node<E> temp = tail;
       if (finger == tail)
       {
           tail = null;
       } else {
           finger.setNext(tail.next());
           tail = finger;
       }
       count--;
       return temp.value();
    }

        @Override
        public int size() {
            return count;
        }

        @Override
        public boolean isEmpty() {
            return size()==0;
        }

        @Override
        public void clear() {
            tail=null;
            count=0;
        }

        @Override
        public E peek() {
            return tail.value();
        }
}